package com.symone.crawler.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.symone.crawler.data.EntityMapper;
import com.symone.crawler.log.Log;
import com.symone.crawler.log.LogFactory;

import java.sql.SQLException;
import java.util.*;

public class EntityUtil {

    private static Log logger = LogFactory.getLog(EntityUtil.class);

    public static Entity setFromJsonObject(Entity entity , JSONObject jsonObject,String name){
        entity.set(name,jsonObject.get(name));
        return entity;
    }
    public static Entity setFromJsonObject(Entity entity , JSONObject jsonObject,String name,Object defaultObject){
        if(jsonObject.get(name) == null ){
            entity.set(name,defaultObject);
        }else {
            entity.set(name, jsonObject.get(name));
        }
        return entity;
    }

    public static void save(JSONObject jsonObject, EntityMapper mapper, Map<String,String> pks){
        String tableName = mapper.getTableName();
        List<String> fields = mapper.getFields();
        save(jsonObject,tableName,fields,pks);
    }

    public static void save(JSONObject jsonObject, EntityMapper mapper,String pk, String pkVal){
        String tableName = mapper.getTableName();
        List<String> fields = mapper.getFields();
        Map<String,String> pks = new HashMap<String,String>();
        pks.put(pk,pkVal);
        save(jsonObject,tableName,fields,pks);
    }

    public static void save(JSONObject jsonObject, EntityMapper mapper){
        if(jsonObject == null){
            logger.error("NO DATA");
            return;
        }
        String tableName = mapper.getTableName();
        List<String> fields = mapper.getFields();
        List<String> pks = mapper.getPks();
        save(jsonObject,tableName,fields,pks);
    }

    public static void save(JSONObject jsonObject, String tableName, List<String> fields,Map<String,String> pks){
        try {
            Entity entity = Entity.create(tableName);
            for (String field : fields){
                field = field.trim();
                if(jsonObject.containsKey(field)){
                    Object val = jsonObject.get(field);
                    if(val == null || val.toString().equals("null") || val.toString().length() == 0){
                        entity.set(field,null);
                    }else {
                        entity.set(field,jsonObject.getStr(field));
                    }
                }
            }
            Entity whereEntity = Entity.create(tableName);
            Set<String> keys = pks.keySet();
            for (String key : keys) {
                key = key.trim();
                whereEntity.set(key, pks.get(key));
                entity.set(key, pks.get(key));
            }
            if( Db.use().count(whereEntity) == 0 ){
                Db.use().insert(entity);
            }else{
                entity.set("update_time", DateUtil.date());
                Db.use().update(entity,whereEntity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void save(JSONObject jsonObject, String tableName, List<String> fields,List<String> pks){
        try {
            Entity entity = Entity.create(tableName);
            for (String field : fields){
                field = field.trim();
                if(jsonObject.containsKey(field)){
                    Object val = jsonObject.get(field);
                    if(val == null || val.toString().equals("null") || val.toString().length() == 0){
                        entity.set(field,null);
                    }else {
                        entity.set(field,jsonObject.get(field));
                    }
                }
            }
            Entity whereEntity = Entity.create(tableName);
            for(String pk : pks){
                pk = pk.trim();
                whereEntity.set(pk,entity.get(pk));
            }
            if( Db.use().count(whereEntity) == 0 ){
                Db.use().insert(entity);
                logger.info("INSERT ENTITY: " + JSONUtil.toJsonStr(entity));
            }else{
                entity.set("update_time",new Date());
                Db.use().update(entity,whereEntity);
                logger.info("UPDATE ENTITY: " + JSONUtil.toJsonStr(entity));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }

    public static void save(Entity entity,String... pks){
        try {
            Entity whereEntity = Entity.create(entity.getTableName());
            for(String pk : pks){
                whereEntity.set(pk,entity.get(pk));
            }
            if( Db.use().count(whereEntity) == 0 ){
                Db.use().insert(entity);
            }else{
                Db.use().update(entity,whereEntity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
